<?php
	include('include/header.php');
	include("include/commands.php");

        //Check if we are coming to this page from an insert query.
        if ($_POST["fromInsert"] == "true") {
            
            //Instantiate connector.
            $connector = new CloudDBInterface();
 
            //Add the new Wash_History tuple now.
            //Get variables.
            $carType = $_POST["carType"];
            $carTypeId = strtok($carType, ":");
            
            $query =    "INSERT INTO Wash_History (carTypeId, date)
                        VALUES ($carTypeId, CURDATE())";

            //Perform the query.
            $connector->insertQuery($query);
            
            //Get the number of possible extras.
            $query =    "SELECT COUNT(*)
                        FROM Extras";
            $resArray = $connector->selectQuery($query);
            $noExtras = $resArray[0]["COUNT(*)"];
            
            //Get washId select the most recent car with that date, most likeley that's the one...
            //incase we have exras to add.
            $query =    "SELECT MAX(Id)
                        FROM Wash_History
                        WHERE carTypeId = $carTypeId
                        AND date = CURDATE()";

            //Get the data.
            $resArray = $connector->selectQuery($query);
            $washId = $resArray[0]["MAX(Id)"];

            //Iterate through options.
            for ($i = 0; $i < $noExtras; $i++) {

                //Check if trigger is set to true. If so, we have a tuple to add
                if (strcmp($_POST["OnextrasHidden".$i], "true") == 0) {

                    //Get the extra Id.
                    $extra = $_POST["extrasHidden".$i];
                    $extraId = strtok($extra, ":");
                    
                    //Create a Wash_Extras tuple.
                    $query =    "INSERT INTO Wash_Extras
                                VALUES ($washId, $extraId)";

                    //Insert the tuple.
                    $connector->insertQuery($query);
                }
            }
                   
        }
        
        if(isset($_GET['car'])) {
		if(!isset($_GET['start']) || !isset($_GET['end'])) {
			$transactions = getWashTransactionsSorted($_GET['car']);
		}
		else {
			$transactions = getWashTransactionsSorted($_GET['car'], $_GET['start'], $_GET['end']);
		}
		
	}
	else {
		$transactions = getWashTransactions();
	}
	
?>

	<div class="body">
	
		<h1> Car Wash History </h1>
		
		<div class="border">
			<form>
			<p>
			Date From <input type="text" name="start"/>
			Date To <input type="text" name="end"/>(yyyy-mm-dd)
			</br>		
			Car Type:
				<select name="car">
					<option>ALL</option>
					<option>Sport</option>
					<option>Compact</option>
					<option>SUV</option>
					<option>Truck</option>
				</select>
			<input type="submit" value="Filter"/>
			</p>
			</form>
		</div>
                <br/>
                <div class="border">
                    <form id="newWashForm" action="wash.php" method="post" onsubmit="return validateWashInsert();">
                        <h4>Add a New Wash</h4>
                        <p>
                            Car Type* <?php carTypes(); ?><br/>
                            Extras <div id="extras"><span id="noneLabel">None</span><?php extras(); ?> </div><br/>
                            <input type="button" name="addExtra" value="Add Extra" onclick="addExtraOption();"/><br/>
                            <input type="button" name="removeExtra" id="removeExtra" value="Remove Extra" onclick="removeExtraOption();" style="display: none"/><br/>
                            
                            <input type="hidden" name="fromInsert" id="fromInsert" value="true"/>
                            <input type="submit" value="Submit"/>
                        </p>
                    </form>
                </div>

		</br>
		
		<?php 
			if(sizeof($transactions) > 0){
					generateTransactionsTable($transactions);
				}
				else{
					echo "<p> There are no car washes registered in the history. </p>";
				}
				
				function generateTransactionsTable($transactions) {
					echo "<table>";
					echo "<tr>";
					echo "<th>Wash Id</th>";
					echo "<th>Car Type</th>";				
					echo "<th>Extra Id</th>";
					echo "<th>Cost($)</th>";
					echo "<th>Date</th>";
					echo "</tr>";
					generateTableBody($transactions);
					echo "</table>";
				}
		?>
	</div>


<?php 
	include('include/footer.php'); 
?>